package com.bao.student.service.impl;

import com.bao.student.dao.impl.StudentDaoImpl;
import com.bao.student.entity.Student;
import com.bao.student.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentServiceImpl implements StudentService {
    public static final StudentDaoImpl studentDao=new StudentDaoImpl();
    public static final Scanner INPUT=studentDao.INPUT;
    public static final ArrayList<Student> students=studentDao.students;

    @Override
    public void addStudent() {
        Student newStudent=createStudent();
        if(newStudent==null){
            return;
        }
        System.out.println("您想要添加的学生信息如下\n"+newStudent);

        if (confirmContinue()){
            studentDao.addStudent(newStudent);
        }else {
            System.err.println("退出操作");
        }
    }

    @Override
    public void deleteStudent() {
        int index=dataYesNo("删除");
        if(confirmContinue()&&index>=0){
            studentDao.deleteStudent(index);
        }else {
            System.err.println("退出操作");
        }

    }

    @Override
    public void updateStudent() {
        int index=dataYesNo("修改");
        if(confirmContinue()&&index>=0){
            studentDao.updateStudent(createStudent(),index);
        }else {
            System.err.println("退出操作");
        }
    }

    @Override
    public void findAllStudent() {
        if(students.size()<=0){
            System.err.println("学生信息为空");
            return;
        }
        studentDao.findAllStudent();
    }

    @Override
    public int findIndex(String findSid) {
        for(int i=0;i<students.size();i++){
            if(students.get(i).getSid().equals(findSid)){
                return i;
            }
        }
        return -1;
    }

    @Override
    public Student createStudent() {
        System.out.println("请输入学生的学号");
        String newSid=INPUT.next();
        if(findIndex(newSid)>=0){
            System.err.println("学号已存在");
            return null;
        }
        System.out.println("请输入学生的姓名");
        String newName=INPUT.next();
        System.out.println("请输入学生的年龄");
        int newAge=INPUT.nextInt();
        System.out.println("请输入学生的生日（yyyy-MM-dd）");
        String newBirthday=INPUT.next();
        return new Student(newSid,newName,newAge,newBirthday);
    }

    @Override
    public boolean confirmContinue() {
        System.out.println("确认继续操作？（y/n）");
        String flag= INPUT.next();
        return "y".equals(flag);
    }

    public int dataYesNo(String str){
        if(students.size()<=0){
            System.err.println("学生信息为空");
            return -1;
        }
        System.out.println("请输入你要"+str+"的学生的学号");
        String findSid=INPUT.next();
        int index=findIndex(findSid);
        if(index<0){
            System.err.println("您要"+str+"的学生信息不存在");
            return index;
        }
        System.out.println("您要"+str+"学生信息如下");
        System.out.println(students.get(index).toString());

        return index;
    }
}
